AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32
local rel = "vehicles/tank_readyfire1.wav"
function ENT:Initialize()
	self.Entity:SetModel(  "models/props_lab/tpplugholder_single.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.yield = 10
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.Entity:SetName("Mining.Laser.Low.Power")
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	self.nextfire = CurTime()
	self.fire = false
	table.insert(servertable["ship"]["weapons"],self.Entity)
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "mininglaser_1" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:OnRemove()
end

function ENT:Think()
	self.Entity:NextThink( CurTime() + 0.25 )
	if self.core then
		if  (self.core:IsValid()) then
			if (self.nextfire < CurTime()) then
				if self.use then self.core.turrets = self.core.turrets - 1 self.use = false end
				if self.fire then
					if (self.core.turrets < self.core.maxturrets) then
						if (self.core.capaciator > 50)then
							if self.targetent:IsValid() then
								if (self.targetent:GetClass() == "Astroid1") then
									self.core.capaciator = self.core.capaciator - 50
									self.core.turrets = self.core.turrets + 1
									self.use = true
									local Startpos =  self.Entity:GetPos() + (self.correct * 50) 
									bullet = {} 
									bullet.Src		= Startpos
									bullet.Dir		= self.correct 
									bullet.Spread		= Vector(0,0,0) 
									bullet.Num		= 1 
									bullet.Damage		= 1 
									bullet.Force		= 1000 
									bullet.Tracer		= 1	 
									bullet.TracerName	= "LaserTracer"
									self.Entity:FireBullets( bullet )
									self.targetent.mining = self.targetent.mining - self.yield
									if (self.targetent.mining<0) then
										Astroidmined(self.targetent,self.Entity)
									end
									self.Entity.fire = false
									self.nextfire = CurTime() + 1
								end
							end
						end
					end
				end
			end				
		else
			self.core = nil
		end					
	else
		self.core = getshipcore(self.Entity)
		if not self.core then
			self.Entity:NextThink( CurTime() + 5 )
			return true
		end
	end
	return true
end

function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end